"use strict";var serial={connectionId:!1,openRequested:!1,openCanceled:!1,bitrate:0,bytesReceived:0,bytesSent:0,failed:0,transmitting:!1,outputBuffer:[],connect:function(e,n,o){function t(){i.openRequested=!0,chrome.serial.connect(e,n,function(e){chrome.runtime.lastError&&console.error(chrome.runtime.lastError.message),e&&!i.openCanceled?(i.connectionId=e.connectionId,i.bitrate=e.bitrate,i.bytesReceived=0,i.bytesSent=0,i.failed=0,i.openRequested=!1,i.onReceive.addListener(function(e){i.bytesReceived+=e.data.byteLength}),i.onReceiveError.addListener(function(e){switch(console.error(e),googleAnalytics.sendException("Serial: "+e.error,!1),e.error){case"system_error":i.failed++||chrome.serial.setPaused(i.connectionId,!1,function(){i.getInfo(function(e){e?e.paused?(console.log("SERIAL: Connection did not recover from last onReceiveError, disconnecting"),GUI.log('Unrecoverable <span style="color: red">failure</span> of serial connection, disconnecting...'),googleAnalytics.sendException("Serial: onReceiveError - unrecoverable",!1),GUI.connected_to||GUI.connecting_to?$("a.connect").click():i.disconnect()):(console.log("SERIAL: Connection recovered from last onReceiveError"),googleAnalytics.sendException("Serial: onReceiveError - recovered",!1),i.failed=0):chrome.runtime.lastError&&console.error(chrome.runtime.lastError.message)})});break;case"timeout":console.log("Receive timout"),c();break;case"device_lost":console.log("Device connection lost"),c();break;case"disconnected":console.log("Disconnected"),c();break;case"break":console.log("Break"),console.log("Retrying to connect from start"),serial.breakReconnect=!0,setTimeout(connection,200);break;default:console.log("Unexpected COM error"),c()}}),console.log("SERIAL: Connection opened with ID: "+e.connectionId+", Baud: "+e.bitrate),o&&o(e)):e&&i.openCanceled?(i.connectionId=e.connectionId,console.log("SERIAL: Connection opened with ID: "+e.connectionId+", but request was canceled, disconnecting"),setTimeout(function(){i.openRequested=!1,i.openCanceled=!1,i.disconnect(function(){c()})},150)):i.openCanceled?(console.log("SERIAL: Connection didn't open and request was canceled"),i.openRequested=!1,i.openCanceled=!1,c()):(i.openRequested=!1,console.log("SERIAL: Failed to open serial port"),googleAnalytics.sendException("Serial: FailedToOpen",!1),c())})}var i=this,r=2,c=function(){r>0?(console.log("Retries left: "+r),r--,setTimeout(t,200)):o&&o(!1)};t()},disconnect:function(e){var n=this;if(n.connectionId){n.emptyOutputBuffer();for(var o=n.onReceive.listeners.length-1;o>=0;o--)n.onReceive.removeListener(n.onReceive.listeners[o]);for(var o=n.onReceiveError.listeners.length-1;o>=0;o--)n.onReceiveError.removeListener(n.onReceiveError.listeners[o]);chrome.serial.disconnect(this.connectionId,function(o){chrome.runtime.lastError&&console.error(chrome.runtime.lastError.message),o?(console.log("SERIAL: Connection with ID: "+n.connectionId+" closed, Sent: "+n.bytesSent+" bytes, Received: "+n.bytesReceived+" bytes"),serial.breakReconnect&&(setTimeout(connection,200),serial.breakReconnect=!1)):(console.log("SERIAL: Failed to close connection with ID: "+n.connectionId+" closed, Sent: "+n.bytesSent+" bytes, Received: "+n.bytesReceived+" bytes"),googleAnalytics.sendException("Serial: FailedToClose",!1)),n.connectionId=!1,n.bitrate=0,e&&e(o)})}else n.openCanceled=!0},getDevices:function(e){chrome.serial.getDevices(function(n){var o=[];n.forEach(function(e){o.push(e.path)}),e(o)})},getInfo:function(e){chrome.serial.getInfo(this.connectionId,e)},getControlSignals:function(e){chrome.serial.getControlSignals(this.connectionId,e)},setControlSignals:function(e,n){chrome.serial.setControlSignals(this.connectionId,e,n)},send:function(e,n){function o(){var e=t.outputBuffer[0].data,n=t.outputBuffer[0].callback;chrome.serial.send(t.connectionId,e,function(e){if(t.bytesSent+=e.bytesSent,n&&n(e),t.outputBuffer.shift(),t.outputBuffer.length){if(t.outputBuffer.length>100){for(var i=0;t.outputBuffer.length>100;)t.outputBuffer.pop(),i++;console.log("SERIAL: Send buffer overflowing, dropped: "+i+" entries")}o()}else t.transmitting=!1})}var t=this;this.outputBuffer.push({data:e,callback:n}),this.transmitting||(this.transmitting=!0,o())},onReceive:{listeners:[],addListener:function(e){chrome.serial.onReceive.addListener(e),this.listeners.push(e)},removeListener:function(e){for(var n=this.listeners.length-1;n>=0;n--)if(this.listeners[n]==e){chrome.serial.onReceive.removeListener(e),this.listeners.splice(n,1);break}}},onReceiveError:{listeners:[],addListener:function(e){chrome.serial.onReceiveError.addListener(e),this.listeners.push(e)},removeListener:function(e){for(var n=this.listeners.length-1;n>=0;n--)if(this.listeners[n]==e){chrome.serial.onReceiveError.removeListener(e),this.listeners.splice(n,1);break}}},emptyOutputBuffer:function(){this.outputBuffer=[],this.transmitting=!1}};